<?php
namespace controller\databases;

class database{
	public $conn;
	public function index()
	{
		$username = 'bdm256530632';	//	数据库用户名
		$password = 'bhy19950608';	//	数据库密码
		$servername = 'bdm256530632.my3w.com';  //	数据库地址
		$db_port = 3306;	//	数据库端口(默认3306)
		$dbname = "bdm256530632_db";
		$this->conn = mysqli_connect($servername, $username, $password, $dbname);  //	链接数据库
		
		if($this->conn){
			return "数据库连接失败" . mysqli_connect_error();
			die("数据库连接失败" . mysqli_connect_error());
		}
		
	}


	public function select($sql)
	{
		$this->index();
		//set utf8
		mysqli_query($this->conn , "set names utf8");		
		$res = mysqli_query($this->conn,$sql);
		if(! $res )
		{
		    return '无法读取数据: ' . mysqli_error($this->conn);
		    die('无法读取数据: ' . mysqli_error($this->conn));
		}

		$results = array();
		while ($row = mysqli_fetch_assoc($res)) {
			$results[] = $row;
		}
		// 释放内存
		mysqli_free_result($res);
		//关闭连接
		mysqli_close($this->conn);
		return $results;
	}


	// 条件查询
	public  function limit($sql,$sqls) 
	{
		$this->index();
		//set utf8
		mysqli_query($this->conn , "set names utf8");
		$res = mysqli_query($this->conn,$sql);
		if(! $res )
		{
		    return '无法读取数据: ' . mysqli_error($this->conn);
		    die('无法读取数据: ' . mysqli_error($this->conn));
		}
		$results = array();

		// 薪资计算公式：基本工资÷日工资(21.75)×出勤天数×(日工资÷月工作日)-罚款+奖金  @ $row['pay']
		// 小数点保留两位
		while ($row = mysqli_fetch_assoc($res)) {
			$results[] = $row;
		}
		
		if($sqls){

			$resu = mysqli_query($this->conn,$sqls);
			$num = mysqli_num_rows($resu);
			$arr = array();
			$arr['rows'] = $results;
			$arr['num'] = $num;
			return $arr;
		}
		// 释放内存
		mysqli_free_result($res);
		//关闭连接
		mysqli_close($this->conn);

		return $results;
	}
	
// 用户条件查询
	public  function limit_user($sql,$sqls) 
	{
		$this->index();
		//set utf8
		mysqli_query($this->conn , "set names utf8");
		
		$res = mysqli_query($this->conn,$sql);
		if(! $res )
		{
		    return '无法读取数据: ' . mysqli_error($this->conn);
		    die('无法读取数据: ' . mysqli_error($this->conn));
		}
		$results = array();
		// 查找用户部门和基本工资
		// 小数点保留两位
		
		while ($row = mysqli_fetch_assoc($res)) {
			$results[] = $row;
		}

		if($sqls){
			$resu = mysqli_query($this->conn,$sqls);
			$num = mysqli_num_rows($resu);
			$arr = array();
			$arr['rows'] = $results;
			$arr['num'] = $num;
			return $arr;

		}
		// 释放内存
		mysqli_free_result($res);
		//关闭连接
		mysqli_close($this->conn);

		return $results;
	}

	public function sql($sql)
	{
		$this->index();
		//set utf8
		mysqli_query($this->conn , "set names utf8");
		$res = mysqli_query($this->conn,$sql);
		if(! $res )
		{
		    return '无法读取数据: ' . mysqli_error($this->conn);
		    die('无法读取数据: ' . mysqli_error($this->conn));
		}
		//关闭连接
		mysqli_close($this->conn);
	}
}